import {MessageInstance} from "antd/es/message/interface";
import {create} from "zustand";

interface IMessageApiStore {
    messageApi: MessageInstance,
    setMessageApi: Function
}
export const useMessageApiStore = create<IMessageApiStore>((set) => ({
    messageApi: null,
    setMessageApi: (messageApi: MessageInstance) => set({messageApi: messageApi})
}))

interface IBgContainerIndex {
    index: number,
    set: Function
}
export const useBgContainerIndexStore = create<IBgContainerIndex>((set) => ({
    index: -1,
    set: (index: number) => {
        set({index: index})
    }
}))

interface IVideoIndex {
    videoIndex: number,
    videoContainerIndex: number,
    videoContainerSize: number,
    set: (containerIndex: number, index: number, containerSize: number) => void,
    // 下一个视频（返回false，表示已经是当前容器最后一个视频）
    next: () => boolean,
    // 上一个视频（返回false，表示已经是第一个视频）
    pre: () => boolean
}

export const useVideoIndexStore = create<IVideoIndex>((set) => ({
    videoIndex: 0,
    videoContainerIndex: 0,
    videoContainerSize: 0,
    set: (containerIndex: number, index: number, containerSize: number) => {
        set({
            videoContainerIndex: containerIndex,
            videoIndex: index,
            videoContainerSize: containerSize,
        })
    },
    next: () => {
        let success = false
        set((state) => {
            if (state.videoIndex + 1 < state.videoContainerSize) {
                success = true
                return {
                    videoIndex: state.videoIndex + 1
                }
            } else {
                return {}
            }
        })
        return success
    },
    pre: () => {
        let success = false
        set((state) => {
            if (state.videoIndex - 1 >= 0) {
                success = true
                return {
                    videoIndex: state.videoIndex - 1
                }
            } else {
                return {}
            }
        })
        return success
    }
}))
